package illustration;

import java.awt.Color;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.ui.ApplicationFrame;
import org.jfree.data.xy.XYSeriesCollection;

public class ClusteringGraph extends ApplicationFrame {

    /**
	 *
	 */
	private JFreeChart chart;
	private static final long serialVersionUID = -5912134007310669192L;

	public ClusteringGraph(Vector<Curve> entries) {

        super("Clustering Graph");

        final XYDataset dataset = createDataset(entries);
        chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 350));
        setContentPane(chartPanel);

    }

    private XYDataset createDataset(Vector<Curve> entries) {

        final XYSeriesCollection dataset = new XYSeriesCollection();
		

        for(int j = 0; j < entries.size(); j++) {
            
            final XYSeries series = new XYSeries(entries.get(j).getLabel());
            
            for(int i = 0; i < entries.get(j).getValues().size(); i++) {
                
                float y = entries.get(j).getValues().get(i).floatValue();
                float x = i + 1;
                
                series.add(x, y);     
            }
            dataset.addSeries(series);
        }
        return dataset;

    }

    private JFreeChart createChart(final XYDataset dataset) {

        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Clustering Graph",
            "Rounds",
            "Values",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );

        chart.setBackgroundPaint(Color.white);

        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

        plot.setRenderer(renderer);

        return chart;

    }
    
    public JFreeChart getChart() {
		return chart;

	}
}
